package com.stack.auto_parts.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.stack.auto_parts.entity.Product;
import com.stack.auto_parts.mapper.ProductMapper;
import com.stack.auto_parts.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
 * @Title: ProductServiceImpl
 * @Author WUYU_MIN
 * @Package com.stack.auto_parts.service.impl
 * @Date 2024/7/10 10:38
 * @description:
 */
@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;

    // 查询所有商品
    @Override
    public List<Product> selectAll() {
        List<Product> productList = productMapper.selectList(null);
        return productList;
    }

    // 上架商品
    @Override
    public String putaway(Product product) {

        // QueryWrapper 条件构造器
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("part_name", product.getPartName());
        queryWrapper.eq("supplier", product.getSupplier());
        if(productMapper.selectOne(queryWrapper) != null){
            return "该零件已经存在了";
        }else {
            product.setUuid(UUID.randomUUID().toString());
            Integer integer = productMapper.insert(product);
            if (integer != 1) {
                return "上架失败";
            }
        }
        return null;
    }

    // 删除商品
    @Override
    public String deleteProduct(String uuid) {
        // QueryWrapper 条件构造器
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uuid", uuid);
        if(productMapper.selectOne(queryWrapper) == null){
            return "该零件不存在";
        }
        else {
            Integer integer = productMapper.deleteById(productMapper.selectOne(queryWrapper));
            if(integer != 1)
                return "零件下架失败";
        }
        return null;
    }

    // 修改商品
    @Override
    public String updateProduct(Product product) {
        // QueryWrapper 条件构造器
        // 查询当前待更新零件是否存在
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uuid", product.getUuid());
        Product currentProduct = productMapper.selectOne(queryWrapper);

        if (currentProduct == null) {
            return "该零件不存在";
        }
        product.setId(currentProduct.getId());
        // 查询当前供应商的其他上架零件中是否存在相同零件名的记录
        QueryWrapper<Product> queryWrapperExisting = new QueryWrapper<>();
        queryWrapperExisting.ne("uuid", product.getUuid()); // 排除当前零件自身
        queryWrapperExisting.eq("part_name", product.getPartName()); // 根据零件名查询
        queryWrapperExisting.eq("supplier", product.getSupplier()); // 当前供应商ID
        Product existingProduct = productMapper.selectOne(queryWrapperExisting);

        if (existingProduct != null) {
            return "修改后的零件名已存在于当前供应商的上架零件中，修改失败";
        }
        // 更新零件信息
        Integer rowsAffected = productMapper.updateById(product);
        if (rowsAffected != 1) {
            return "零件信息修改失败";
        }
        return null;
    }

    // 根据条件查询商品,零件名，供应商
    @Override
    public List<Product> selectByCondition(String part_name, String supplier) {

        List<Product> productList = productMapper.listByCondition(part_name, supplier);
        return productList;
    }

    @Override
    public Integer getNumber(String part_name, String supplier) {

        Product number = productMapper.getNumber(part_name, supplier);
        return number.getInventory();
    }

    @Override
    public void updataNumber(String partName, String supplier, int number) {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("part_name", partName);
        queryWrapper.eq("supplier", supplier);
        Product currentProduct = productMapper.selectOne(queryWrapper);
        currentProduct.setInventory(number);
        productMapper.updateById(currentProduct);
    }
}
